package com.xiaoxu.intranetweb.util;

import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;


    public class SzResult extends LinkedHashMap<String, Object> implements Serializable {
        private static final long serialVersionUID = 1L;
        public static final int CODE_SUCCESS = 200;
        public static final int CODE_ERROR = 500;
        public SzResult() {
        }
        public SzResult(int code, String msg, Object result) {
            this.setCode(code);
            this.setMsg(msg);
            this.setResult(result);
            this.setTimestamp(this.getTimestamp());
        }
//        获取当前时间戳
        public  long getTimestamp() {
            return System.currentTimeMillis();
        }
        public SzResult(Map<String, ?> map) {
            this.setMap(map);
        }

        public Integer getCode() {
            return (Integer)this.get("code");
        }

        public String getMsg() {
            return (String)this.get("message");
        }

        public Object getResult() {
            return this.get("result");
        }

        public SzResult setCode(int code) {
            this.put("code", code);
            return this;
        }

        public SzResult setMsg(String msg) {
            this.put("message", msg);
            return this;
        }

        public SzResult setResult(Object result) {
            this.put("result", result);
            return this;
        }

        public SzResult setTimestamp(long timestamp) {
            this.put("resultTimestamp", timestamp);
            return this;
        }

        public SzResult set(String key, Object data) {
            this.put(key, data);
            return this;
        }

        public <T> T get(String key, Class<T> cs) {
            return SaFoxUtil.getValueByType(this.get(key), cs);
        }

        public SzResult setMap(Map<String, ?> map) {
            Iterator var2 = map.keySet().iterator();

            while(var2.hasNext()) {
                String key = (String)var2.next();
                this.put(key, map.get(key));
            }

            return this;
        }
//200但是error
        public static SzResult error200(Object data) {
            return new SzResult(200, "error", data);
        }
        public static SzResult Success() {
            return new SzResult(200, "success",  (Object)null);
        }

        public static SzResult success(Object data) {
            return new SzResult(200, "success", data);
        }

        public static SzResult code(int code) {
            return new SzResult(code, (String)null, (Object)null);
        }

//        public static SzResult data(Object data) {
//            return new SzResult(200, "success", data);
//        }

        public static SzResult Error( String msg) {
            return new SzResult(500, msg, (Object)null);
        }

        public static SzResult error(Object data) {
            return new SzResult(500, "error",data);
        }
//        token报错的时候的返回值
        public static SzResult tokenError(Object data) {
            return new SzResult(10011, "error",data);
        }
        public static SzResult get(int code, String msg, Object data) {
            return new SzResult(code, msg, data);
        }

        public String toString() {
            return "{\"code\": " + this.getCode() + ", \"message\": " + this.transValue(this.getMsg()) + ", \"result\": " + this.transValue(this.getResult()) +  ", \"resultTimestamp\": " + this.getTimestamp() +"}";
        }

        private String transValue(Object value) {
            return value instanceof String ? "\"" + value + "\"" : String.valueOf(value);
        }
    }


